package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class MainMenu {

    private final Library library;

    private User loggedInUser;

    MainMenu(Library library){
        this.library = library;
    }

    void displayMenu() {
        System.out.println("\nMain Menu:");
        String formatString = "%-10s %-30s \n";
        System.out.printf(formatString, "Option #", "Description");
        String divLine = "-----------------------------------------------";
        System.out.println(divLine);
        for (Options option : Options.values()){
            System.out.printf(formatString, option.ordinal(), option.getDescription());
        }
        System.out.println(divLine + "\n");
    }

    String readUserInput()
            throws IOException {
        BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
        return br.readLine();
    }

    void getUserChoice() {
        while (true) {
            try {
                System.out.println("Enter your chosen option # (q to quit):");
                String userInput = readUserInput();
                if (userInput.equals("q"))
                    break;
                int menuOption = Integer.parseInt(userInput);
                executeOption(menuOption);
                displayMenu();
            } catch (InvalidMenuOptionException e) {
                System.out.println("Select a valid option!");
            } catch (NumberFormatException e) {
                System.out.println("Select a valid option!");
            } catch (IOException e) {
                System.out.println("Select a valid option!");
            } catch (NotLoggedInException e) {
                System.out.println("You must log in first.");
            }

        }
    }



    void displayUserInfo() {
        System.out.println("\nName:  " + loggedInUser.getName());
        System.out.println("Email: " + loggedInUser.getEmailAddress());
        System.out.println("Phone: " + loggedInUser.getPhoneNumber() + "\n");
    }

    boolean userIsLoggedIn() {
        return loggedInUser != null;
    }

    void submitLoginRequest(String customerNumber, String password)
            throws UnsuccessfulLoginAttemptException {
        loggedInUser = library.login(customerNumber, password);
        if (!userIsLoggedIn()) {
            throw new UnsuccessfulLoginAttemptException(customerNumber);
        }
    }

    void login() {
        try {
            System.out.println("Enter your customer number in the form XXX-XXXX:");
            String customerNumber = readUserInput();
            System.out.println("Enter your password:");
            String password = readUserInput();
            submitLoginRequest(customerNumber, password);
            System.out.println("Login successful.");
        } catch (IOException e) {
            System.out.println("Login failed.");
        } catch (UnsuccessfulLoginAttemptException e) {
            System.out.println("Attempt to login with customer number "
                    + e.getCustomerNumber() + " failed.");
        }
    }

    void executeOption(int menuOption)
            throws InvalidMenuOptionException, NotLoggedInException{
        try {
            executeOption(Options.values()[menuOption]);
        } catch (ArrayIndexOutOfBoundsException e) {
            throw new InvalidMenuOptionException(menuOption);
        }
    }

    void executeOption(Options option)
            throws NotLoggedInException {
        System.out.println("Option " + option.ordinal() +
                " chosen: " + option.getDescription());
        if (option == Options.LISTBOOKS) {
            library.displayBookList();
        }
        else if (option == Options.LISTMOVIES) {
            library.displayMovieList();
        }
        else if (option == Options.LOGIN) {
            login();
        }
        else if (!userIsLoggedIn()) {
            throw new NotLoggedInException();
        }
        else if (option == Options.CHECKOUTBOOK) {
            library.checkOutBook(loggedInUser);
        }
        else if (option == Options.RETURNBOOK) {
            library.returnBook(loggedInUser);
        }
        else if (option == Options.CHECKOUTMOVIE) {
            library.checkOutMovie(loggedInUser);
        }
        else if (option == Options.DISPLAYUSERINFO) {
            displayUserInfo();
        }
    }




}
